/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bff.slimserver.musicjukebox.ui.components.favorites;

import com.bff.bjj.ui.component.MusicListCellRenderer;
import com.bff.bjj.ui.component.ColorProperties;
import com.bff.slimserver.musicjukebox.lib.JukeboxProperties;
import java.awt.Component;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JList;
import org.bff.slimserver.SlimServer;
import org.bff.slimserver.exception.SlimConnectionException;
import org.bff.slimserver.musicobjects.radio.SlimAvailableRadio;

/**
 *
 * @author bfindeisen
 */
public class FavoritesListCellRenderer extends MusicListCellRenderer {

    private SlimServer slimServer;

    private HashMap<SlimAvailableRadio, FavoritesCellPanelIface> radioPanels;

    public FavoritesListCellRenderer() {
        super();
        radioPanels = new HashMap<SlimAvailableRadio, FavoritesCellPanelIface>();
        try {
            setSlimServer(JukeboxProperties.getInstance().getSlimServer());
        } catch (SlimConnectionException ex) {
            Logger.getLogger(FavoritesListCellRenderer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        if (value instanceof SlimAvailableRadio) {
            SlimAvailableRadio radio = (SlimAvailableRadio) value;

            FavoritesCellPanelIface panel = radioPanels.get(radio);

            if (panel == null) {
                panel = new FavoritesCellPanel(radio);
                radioPanels.put(radio, panel);
            }

            if (panel != null) {
            if ((index % 2) == 0) {
                panel.setEven();
            } else {
                panel.setOdd();
            }

            if (!isSelected) {
                panel.setForeground(ColorProperties.LIST_FOREGROUND);
                panel.setNotSelected();
            } else {
                panel.setSelected();
            }
        }

            return panel.getPanel();
        } else {
            setText(value.toString());
            setColors(list, index, isSelected);
            return this;
        }

    }

    /**
     * @return the slimServer
     */
    public SlimServer getSlimServer() {
        return slimServer;
    }

    /**
     * @param slimServer the slimServer to set
     */
    public void setSlimServer(SlimServer slimServer) {
        this.slimServer = slimServer;
    }
}
